// ApplicationsManager.cpp : implementation of the CApplicationsManager class
//
#include "stdafx.h"
#include "ApplicationsManager.h"

CApplicationsManager::CApplicationsManager()
{
	// TODO: add member initialization code here
	m_winApplicationList=new GList();
	m_applicationAccountsList=new GList();
}
CApplicationsManager::~CApplicationsManager()
{
	delete m_winApplicationList;
	delete m_applicationAccountsList;
}

CWinApplication& CApplicationsManager::GetWinApplicationByName(CString nWinAppName)
{
	GListIterator itWinApp(m_winApplicationList);
	while(itWinApp())
	{
		CWinApplication* pWinApp = (CWinApplication *)itWinApp++;
		if(pWinApp->GetName().Compare((LPCTSTR)nWinAppName)==0)
			return *pWinApp;
	}
}

CWinApplication& CApplicationsManager::GetWinApplicationByWindowTitle(CString nWinTitle)
{
	GListIterator itWinApp(m_winApplicationList);
	while(itWinApp())
	{
		CWinApplication* pWinAp=(CWinApplication *)itWinApp++;
		GListIterator itWinForm(pWinAp->GetWinFormList());
		while(itWinForm())
		{
			CWinForm *pWinForm = (CWinForm *)itWinForm++;
			GListIterator itWinTitle(pWinForm->GetWindowTitleList());
			while(itWinTitle())
			{
				CWindowTitle *pWinTitle = (CWindowTitle *)itWinTitle++;
				//TODO comparaison with regex
				if(pWinTitle->GetValue().Compare((LPCTSTR)nWinTitle)==0)
				{
					return *pWinAp;
				}
			}
		}
	}
}



CAccount& CApplicationsManager::GetAccountById(CString nIdAccount)
{
	GListIterator itApAcc(m_applicationAccountsList);
	while(itApAcc())
	{
		CApplicationAccounts * pApAcc = (CApplicationAccounts *)itApAcc++;
		GListIterator itAcc(pApAcc->GetAccounts());
		while(itAcc())
		{
			CAccount *pAccount = (CAccount *)itAcc++;
			if(pAccount->GetId().Compare((LPCTSTR)nIdAccount)==0)
				return *pAccount;
		}
	}
}

GList* CApplicationsManager::GetAccountListByAppName(CString nAppName)
{
	GListIterator itApAcc(m_applicationAccountsList);
	while(itApAcc())
	{
		CApplicationAccounts * pApAcc = (CApplicationAccounts *)itApAcc++;
		if(pApAcc->GetApplicationName().Compare((LPCTSTR)nAppName)==0)
			return pApAcc->GetAccounts();
	}
}
GList* CApplicationsManager::GetWinAppList()
{
	return m_winApplicationList;
}

GList* CApplicationsManager::GetApplicationAccountsList()
{
	return  m_applicationAccountsList;
}

void CApplicationsManager::LoadApplicationsFromRepository(CString nRepository)
{
	m_winApplicationList->RemoveAll();
	CFileFind finder;
	BOOL bWorking = finder.FindFile(_T("*_oesso.xml*"));
	while (bWorking)
	{
		CWinApplication* p_winApp=new CWinApplication();
		bWorking = finder.FindNextFile();
		p_winApp->FromXMLFile((LPCTSTR)finder.GetFileName());
		m_winApplicationList->AddHead(p_winApp);
	}
}

void CApplicationsManager::LoadApplicationAccountsFromRepository(CString nRepository)
{
	CFileFind finder;
	BOOL bWorking = finder.FindFile(_T("*_account.xml*"));	
	while (bWorking)
	{
		CApplicationAccounts* p_appAc=new CApplicationAccounts();
		bWorking = finder.FindNextFile();
		p_appAc->FromXMLFile((LPCTSTR)finder.GetFileName());
		m_applicationAccountsList->AddHead(p_appAc);
	}
}

